<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>有效的括号</title>
</head>
<body>
  <script>
    var s = '()'
    function fn(s) {
      if(s.length%2 !== 0) return false
      const map = new Map([
          ['(',')'],
          ['{','}'],
          ['[',']']
      ])
      let stk = []
      for( ch of s){
          if(map.has(ch)){
              stk.push(ch)
          }else{
              if(map.get(stk[stk.length - 1]) !== ch || stk.length == 0 ) return false
              else stk.pop()
          }
      }
      return stk.length == 0
    }
    console.log(fn(s))
  </script>
</body>
</html>